package com.xyh.cloudorder.controller;


import com.xyh.cloudorder.pojo.order.param.*;
import com.xyh.cloudorder.service.order.IOrder;
import com.xyh.cloudorder.utils.JSONResult;
import com.xyh.cloudorder.utils.ValidatorUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private IOrder iOrder;

    @RequestMapping(value = "/newOrder",method = RequestMethod.POST)
    public JSONResult newOrder(@RequestBody newOrderParam param){
        //验证接收的参数
        try {
            ValidatorUtils.validateEntity(param);
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            return JSONResult.errorMsg("参数验证失败！原因：" + e.getMessage());
        }

        try {
            iOrder.newOrder(param);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.errorMsg("生成订单失败！原因：" + e.getMessage());
        }
        return JSONResult.ok();
    }

    @RequestMapping(value = "/updateOrder",method = RequestMethod.POST)
    public JSONResult updateOrder(@RequestBody updateOrderStatusParam param){
        //验证接收的参数
        try {
            ValidatorUtils.validateEntity(param);
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            return JSONResult.errorMsg("参数验证失败！原因：" + e.getMessage());
        }
        String status = param.getStatus();
        if("0".equals(status) || "1".equals(status) || "2".equals(status) || "3".equals(status) || "4".equals(status)){

        }else{
            return JSONResult.errorMsg("无效的状态参数！");
        }
        try {
            iOrder.updateOrderStatus(param);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.errorMsg("更新订单信息失败！原因：" + e.getMessage());
        }
        return JSONResult.ok();
    }


    //更新订单的 报销金额 自费金额  是否打印发票等信息
    @RequestMapping(value = "/updateOrderOther",method = RequestMethod.POST)
    public JSONResult updateOrderOther(@RequestBody updateOrderOtherParam param){
        //验证接收的参数
        try {
            ValidatorUtils.validateEntity(param);
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            return JSONResult.errorMsg("参数验证失败！原因：" + e.getMessage());
        }
        try {
            iOrder.updateOrderOther(param);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.errorMsg("更新订单信息失败！原因：" + e.getMessage());
        }
        return JSONResult.ok();
    }

    @RequestMapping(value = "/queryOrderAllInfo",method = RequestMethod.POST)
    public JSONResult queryOrderAllInfo(@RequestBody queryOrderAllInfoParam param){
        //验证接收的参数
        try {
            ValidatorUtils.validateEntity(param);
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            return JSONResult.errorMsg("参数验证失败！原因：" + e.getMessage());
        }
        Map map = new HashMap();
        try {
            map = iOrder.queryOrderAllInfo(param);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.errorMsg("查询订单信息失败！原因：" + e.getMessage());
        }
        return JSONResult.ok(map);
    }

    @RequestMapping(value = "/queryOrderAllInfoByBz1",method = RequestMethod.POST)
    public JSONResult queryOrderAllInfoByBz1(@RequestBody queryOrderAllInfoByBz1Param param){
        //验证接收的参数
        try {
            ValidatorUtils.validateEntity(param);
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            return JSONResult.errorMsg("参数验证失败！原因：" + e.getMessage());
        }
        Map map = new HashMap();
        try {
            map = iOrder.queryOrderAllInfoByBz1(param);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.errorMsg("查询订单信息失败！原因：" + e.getMessage());
        }
        return JSONResult.ok(map);
    }

    @RequestMapping(value = "/queryOrderAllInfoBySfzh",method = RequestMethod.POST)
    public JSONResult queryOrderAllInfoBySfzh(@RequestBody queryOrderAllInfoBySfzhParam param){
        //验证接收的参数
        try {
            ValidatorUtils.validateEntity(param);
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            return JSONResult.errorMsg("参数验证失败！原因：" + e.getMessage());
        }
        Map map = new HashMap();
        try {
            map = iOrder.queryOrderAllInfoBySfzh(param);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.errorMsg("查询订单信息失败！原因：" + e.getMessage());
        }
        return JSONResult.ok(map);
    }
}
